﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DcsGauges.Gui.Gauges.Interfaces;
using System.Diagnostics;
using DcsGauges.Shared.DataStructures;

namespace DcsGauges.Data
{
    internal class MultiTwoWayDataConnector : IDataConnector
    {
        #region Fields & Properties

        private IMultiTriggerGauge              gauge               = null;
        private IDcsA10DataModel                model               = null;
        private bool                            updateDisabled      = false;

        #endregion


        #region Constructors

        public MultiTwoWayDataConnector(IMultiTriggerGauge gauge, IDcsA10DataModel dataModel)
        {
            this.gauge              = gauge;
            this.model              = dataModel;
            this.gauge.TriggerEvent += new EventHandler<TriggerValueEventArgs>(OnGaugeHandleTriggerEvent);
        }

        #endregion


        #region Interface Implemenation

        public void Update()
        {
            if (this.updateDisabled)
            {
                return;
            }

            foreach (DcsProperty prop in this.gauge.Properties)
            {
                float value             = this.model.GetProperty(prop);
                this.gauge.SetValue(prop, value);
            }
        }

        #endregion

        #region Event Handlers

        private void OnGaugeHandleTriggerEvent(object sender, TriggerValueEventArgs e)
        {
            Debug.Assert(this.gauge.Properties.Contains(e.DcsProperty), "The event has an dcs property that is not contained within the associated gauge");
            
            this.model.IssueCommand(e.DcsProperty, e.NewValue);

#warning for testing update is disabled indefinitely
            this.updateDisabled = true; 
        }

        #endregion
    }
}
